﻿
@{
    ViewBag.Title = "部门管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-header">{{lang.Field.DepartmentList}}</div>
            <div class="layui-card-body">
                <div id="departmentTree"></div>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['tree', 'layer', 'index'], function () {
                var tree = layui.tree,
                    layer = layui.layer;

                var editId = 0,
                    parentId = 0;
                queryTree();
                function queryTree() {
                    layer.load();
                    $.ajax({
                        url: "/Settings/QueryDepartmentsTree",
                        complete: function () {
                            layer.closeAll();
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 6 });
                                return;
                            }

                            var treeData = new Array();
                            treeData.push({
                                id: 0,
                                title: "全部部门",
                                spread: true,
                                children: res.Data
                            });
                            initTree(treeData);
                        }
                    });
                }
                function initTree(treeData) {
                    tree.render({
                        elem: '#departmentTree'
                        , data: treeData
                        , edit: ['add', 'update', 'del'] //操作节点的图标
                        , operate: function (obj) {
                            var type = obj.type; //得到操作类型：add、edit、del
                            var data = obj.data; //得到当前节点的数据
                            var elem = obj.elem; //得到当前节点元素

                            if (type === 'add') {//新增部门
                                return editDepartment(0, data.id, "未命名");
                            } else if (type === 'update') {//编辑部门
                                editDepartment(data.id, 0, data.title.trim());
                            } else if (type === 'del') {//删除部门
                                deleteDepartment(data.id);
                            };
                        }
                    });
                }

                function editDepartment(departmentId, parentId, departmentName) {
                    var loadId = layer.load();
                    $.ajax({
                        async: false,
                        url: "/Settings/EditDepartment",
                        data: {
                            PkId: departmentId,
                            ParentId: parentId,
                            DepartmentName: departmentName
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                queryTree();
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            if (departmentId > 0) {
                                layer.msg("编辑成功！", { icon: 6 });
                            }
                            departmentId = res.Data.PkId;
                        }
                    });
                    return departmentId;
                }

                function deleteDepartment(departmentId) {
                    var loadId = layer.load();
                    $.ajax({
                        url: "/Settings/DeleteDepartment",
                        data: {
                            departmentId: departmentId
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                parent.layui.layer.msg(res.ErrMsg, { icon: 5 });
                                queryTree();
                                return;
                            }
                            parent.layui.layer.msg("删除成功！", { icon: 6 });
                            queryTree();
                        }
                    });
                }
            });
        }
    </script>
}
